/*
 * Copyright (C) 2016. Tripint Information Technology Co., Ltd. All rights reserved. *
 * Authors:lirichen
 * File:.java
 * This software, including documentation, is protected by copyright controlled
 * by Tripint Information Technology Co., Ltd. All rights are reserved.
 */

package com.ganee.baselib.httprequest.exception;


import android.content.Intent;

import com.alibaba.android.arouter.launcher.ARouter;
import com.ganee.baselib.arouter.Constance;
import com.ganee.baselib.httprequest.base.BaseApplication;
import com.ganee.baselib.httprequest.cache.ACache;
import com.ganee.baselib.httprequest.cache.EnumKey;

/**
 * 错误码
 */
public class ApiException extends RuntimeException {

    public static final String ERROR_CODE_001 = "0001";


    public ApiException(String resultCode, String msg) {
        this(getApiExceptionMessage(resultCode, msg));
    }

    public ApiException(String detailMessage) {
        super(detailMessage);
    }

    /**
     * 由于服务器传递过来的错误信息直接给用户看的话，用户未必能够理解
     * 需要根据错误码对错误信息进行一个转换，在显示给用户
     *
     * @param code
     * @return
     */
    private static String getApiExceptionMessage(String code, String msg) {
        String message = "";
        switch (code) {
            case ERROR_CODE_001:
                ACache.get(BaseApplication.getInstance()).put(EnumKey.User.USER_TOKEN, "");
                ARouter.getInstance().build(Constance.ACTIVITY_URL_LOGIN).withFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK).navigation();
                break;
            default:
                if (msg != null) {
                    message = msg;
                } else {
                    message = "System Error";
                }

                break;
        }
        return message;
    }


}

